<?php

namespace app\common\model\shop;

use app\common\model\BaseModel;

/**
 * 门店标签绑定
 * Class ShopTagsBind
 * @package app\common\model\shop
 */
class ShopTagsBindModel extends BaseModel
{
    //
    protected $name = 'shop_tags_bind';

    // 设置字段信息
    protected $schema = [
        'shop_id' => 'int',        // 店铺ID
        'tags_id' => 'int',        // 标签ID
    ];

    /**
     * 根据标签ID,获取对应的的店铺ID
     * @param array $tagId 标签ID
     * @return mixed  [ 'id'=>'xx,xx,xx']
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getShopIdForTagId(array $tagId)
    {
        $list = $this->db()->whereIn('tags_id', $tagId)
            ->field(["tags_id, string_agg(cast(shop_id as varchar), ',') as ids"])->group('tags_id')->select();
        if (!empty($list)) {
            $list = array_column($list, 'ids', 'tags_id');
        }
        return $list;
    }
}
